/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.regex.Pattern;

/**
 * <p>Project: javaseapp - Ex4
 * <p>Powered by webrx On 2021-08-25 11:27:41
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 16
 */
public class Ex4 {
    public static void main(String[] args) {
        Set<String> set = new HashSet<String>();
        while (set.size() < 11) {
            set.add(getStr());
            System.out.println(set.size());
        }
        set.forEach(System.out::println);

        //for (int i = 0; i < 11; i++) {
        //   System.out.printf("%s = 100%n", getStr());
        //}

    }

    public static String getStr() {
        String str = "";
        var sbu = new StringBuilder("1");
        while (true) {
            var rand = new Random();
            var op = List.of("", "+", "-");
            for (int i = 2; i <= 9; i++) {
                sbu.append(String.format("%s%d", op.get(rand.nextInt(op.size())), i));
            }
            String s = sbu.toString();
            String st = s.replaceAll("(\\+|-)", ",$1");
            Pattern pattern = Pattern.compile(",");
            int sum = pattern.splitAsStream(st).map(Integer::parseInt).reduce(0, Integer::sum);
            if (sum == 100) {
                str = s;
                break;
            } else {
                sbu.delete(1, sbu.length());
            }
        }
        return str;

    }

    public static void demo(String[] args) {
        var sbu = new StringBuilder("1");
        var rand = new Random();
        var op = List.of("", "+", "-");
        for (int i = 2; i <= 9; i++) {
            sbu.append(String.format("%s%d", op.get(rand.nextInt(op.size())), i));
        }
        String str = "1-2-3+45+6+7-8-9";
        str = str.replaceAll("(\\+|-)", ",$1");
        Pattern pattern = Pattern.compile(",");
        int sum = pattern.splitAsStream(str).map(Integer::parseInt).reduce(0, Integer::sum);
        System.out.println(sbu);

        /*
        Pattern pattern = Pattern.compile("-?\\d+");

        Matcher m = pattern.matcher(str);
        while (m.find()) {
            System.out.println(m.group());
        }*/

    }
}
